<template>
  <div v-if="listOrDetail" class="flex flex-col h-full">
    <SearchContainer @search="queryList" @reset="() => (formState = {})">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="type">
          <a-radio-group v-model:value="tabActiveKey" button-style="solid" class="w-96 flex mx-0" @change="TabRadioChange">
            <a-radio-button v-for="(item, index) in options" :key="index" :value="item.bh">{{ item.mc }}</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item name="zjm" class="ml-4">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="编码/名称/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <BaseTable
        :loading="loading"
        :column-code="columnNum"
        fill
        :data-source="tableData"
        bordered
        :scroll="{ x: 2200 }"
        :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
        @change="onPageChange"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'dj'">
            {{ record.dj?.toFixed(3) }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg }" />
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-switch v-model:checked="record.status" :checked-value="'1'" :un-checked-value="'0'" @change="onChangeStatus(record, $event)" />
          </template>
          <template v-if="column.dataIndex === 'certificateTime'">
            <span v-if="record.certificateStartTime">{{ record.certificateStartTime }}~{{ record.certificateEndTime }}</span>
            <span v-else>-</span>
          </template>
          <template v-if="column.dataIndex === 'diopter'">
            <span v-if="record.diopterMin">{{ record.diopterMin }}~{{ record.diopterMax }}</span>
            <span v-else>-</span>
          </template>
          <template v-if="column.dataIndex === 'astigmatism'">
            <span v-if="record.astigmatismMin">{{ record.astigmatismMin }}~{{ record.astigmatismMax }}</span>
            <span v-else>-</span>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button v-if="tabActiveKey === 61 || tabActiveKey === 62" type="link" size="small" @click="toDetail(record)"> 明细 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
  <Detail v-else v-model:open="listOrDetail" :row-data="detailRowData" :tab-active-key="tabActiveKey" />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import Detail from './detail/index'
// 镜片 61  隐形眼镜 62  角塑 63
const listOrDetail = ref(true)
const detailRowData = ref({})
const loading = ref(false)
const tableData = ref([])
const formRef = ref(null)
const formState = ref({})
const options = ref([])
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const columnNum = ref('000225')
const tabActiveKey = ref(61)

const getProjectList = async () => {
  const { code, msg, data } = await post('/optometry/ooadolescentcheck/getDictOrderCategoryListById', { ids: [61, 62, 63] })
  if (code !== 0) return message.error(msg)
  options.value = data
}

const onChangeStatus = async (row, status) => {
  const { code, msg } = await post('/operation/opex/doUpdateOoProductStatus', {
    id: row.id,
    status
  })
  code === 0 ? message.success('操作成功') : message.error(msg || '操作失败')
  queryList()
}

const queryList = async () => {
  loading.value = true
  const { code, data, total, msg } = await post('/operation/opex/getOoProductList', {
    splb: tabActiveKey.value,
    zjm: formState.value.zjm,
    pageNum: page.value.current,
    pageSize: page.value.pageSize
  })
  if (code !== 0) return message.error(msg)
  tableData.value = data
  page.value.total = total
  loading.value = false
}
const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  queryList()
}

const toDetail = (row) => {
  detailRowData.value = row
  listOrDetail.value = false
}
onMounted(() => {
  getProjectList()
  queryList()
})
const TabRadioChange = (e) => {
  if (e.target.value === 61) {
    //镜片
    columnNum.value = '000225'
  } else if (e.target.value === 62) {
    //隐形眼镜
    columnNum.value = '000226'
  } else {
    //角塑
    columnNum.value = '000227'
  }
  queryList()
}
</script>
<style lang="less" scoped>
:deep(.ant-radio-button-wrapper) {
  flex: 1;
  color: var(--primary-color);
  text-align: center;
  border-color: var(--primary-color);
  &::before {
    background-color: var(--primary-color);
  }
}
.ant-picker {
  width: 100%;
}
</style>
